package com.gesee.zyspps.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.gesee.zys.R;
import com.gesee.zyspps.ZSApplication;
import com.gesee.zyspps.adapter.FswAdapter;
import com.gesee.zyspps.adapter.TeZhengAdapter;
import com.gesee.zyspps.db.CmsFeature;
import com.gesee.zyspps.db.CmsPoints;
import com.gesee.zyspps.db.CmsSpecialLine;
import com.gesee.zyspps.db.CmsPointsDao.Properties;
import com.gesee.zyspps.utils.LogUtil;
import com.gesee.zyspps.utils.StringUtil;
import com.gesee.zyspps.utils.ToastUtil;

/**
 * 
 * @ClassName: DrawPointActivity
 * @Description: TODO(绘制管点属性)
 * @author fujianxiong
 * @date 2015年8月15日 上午10:25:43
 * 
 */
public class DrawPointActivity extends BaseActivity implements OnClickListener
{
    private Button btnAccept, btnCancel;
    private boolean flag = false;
    private boolean flag1 = false;
    private boolean flag2 = false;
    private boolean flag3 = false;
    private boolean flag4 = false;
    
    private EditText et_gdbh;// 管点编号
    // private CheckBox cb_kj; // 是否可见
    private EditText et_tezheng;// 特征
    private EditText et_fsw;// 附属物
    private EditText et_jdms;// 井底埋深
    private EditText et_jggg;// 井盖规格
    private EditText et_jgcz;// 井盖材质
    // private EditText et_jgcc;// 井盖尺寸
    private EditText et_jtsd;// 井桶深度
    private EditText et_jscc;// 井室尺寸
    private EditText et_xzuobiao;// X坐标
    private EditText et_yzuobiao;// Y坐标
    private EditText et_qsdw;// 权属单位
    private EditText et_dlmc;// 道路名称
    private EditText et_bz;// 备注
    private EditText et_ydqk;// 淤堵情况

    private Spinner sp_tezheng;// 特征
    private Spinner sp_fsw;// 附属物
    private Spinner sp_jgcz;// 井盖材质
    private Spinner sp_qsdw;// 权属单位
    private Spinner sp_ydqk;// 淤堵情况
    
    private EditText et_jslx,et_tcsss,et_tcsns,et_jcjdj,et_jcjzpbh;

    private String[] tzItems;
    private String[] fswItems;
    private String[] jgczItems;
    private String[] qsdwItems;
    private String[] ydqkItems;

   // private LinearLayout ll_dlts;// 电缆条数
   // private LinearLayout ll_yyks;// 已用孔数
   // private LinearLayout ll_zks;// 总孔数
    
   // private EditText et_dlts;
    //private EditText et_yyks;
    ///private EditText et_zks;

    // private ArrayAdapter<String> tzAdapter;
    // private ArrayAdapter<String> fswAdapter;

    private FswAdapter fswAdapter, tzAdapter,jgczAdapter,qsdwAdapter,ydqkAdapter;
    /*private ArrayAdapter<String> jgczAdapter;
    private ArrayAdapter<String> qsdwAdapter;
    private ArrayAdapter<String> ydqkAdapter;*/

    private String D_X = null; // X坐标
    private String D_Y = null;// Y坐标
    private String mapNo = null;
    private String selectSubCode = null;
    private int size;
    private String WDeep, Mtype, Wcover, Wdepth, Hsize, BCode, RoadName,Subsid,Feature;
    private List<CmsSpecialLine> specialLineList;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw_point);
        findView();
        initView();
    }

    private void findView()
    {
        btnAccept = (Button) findViewById(R.id.btnAccept);
        btnCancel = (Button) findViewById(R.id.btnCancel);
        et_gdbh = (EditText) findViewById(R.id.et_gdbh);
        // cb_kj = (CheckBox) findViewById(R.id.cb_kj);
        et_tezheng = (EditText) findViewById(R.id.et_tezheng);
        et_fsw = (EditText) findViewById(R.id.et_fsw);
        et_jdms = (EditText) findViewById(R.id.et_jdms);
        et_jggg = (EditText) findViewById(R.id.et_jggg);
        et_jgcz = (EditText) findViewById(R.id.et_jgcz);
        // et_jgcc = (EditText) findViewById(R.id.et_jgcc);
        et_jtsd = (EditText) findViewById(R.id.et_jtsd);
        et_jscc = (EditText) findViewById(R.id.et_jscc);
        et_xzuobiao = (EditText) findViewById(R.id.et_xzuobiao);
        et_yzuobiao = (EditText) findViewById(R.id.et_yzuobiao);
        et_qsdw = (EditText) findViewById(R.id.et_qsdw);
        et_dlmc = (EditText) findViewById(R.id.et_dlmc);
        et_bz = (EditText) findViewById(R.id.et_bz);
        et_ydqk = (EditText) findViewById(R.id.et_ydqk);
        et_jslx = (EditText) findViewById(R.id.et_jslx);
        et_tcsss = (EditText) findViewById(R.id.et_tcsss);
        et_tcsns = (EditText) findViewById(R.id.et_tcsns);
        et_jcjdj = (EditText) findViewById(R.id.et_jcjdj);
        et_jcjzpbh = (EditText) findViewById(R.id.et_jcjzpbh);

        sp_tezheng = (Spinner) findViewById(R.id.sp_tezheng);
        sp_fsw = (Spinner) findViewById(R.id.sp_fsw);
        sp_jgcz = (Spinner) findViewById(R.id.sp_jgcz);
        sp_qsdw = (Spinner) findViewById(R.id.sp_qsdw);
        sp_ydqk = (Spinner) findViewById(R.id.sp_ydqk);

        /*ll_dlts = (LinearLayout) findViewById(R.id.ll_dlts);
        ll_yyks = (LinearLayout) findViewById(R.id.ll_yyks);
        ll_zks = (LinearLayout) findViewById(R.id.ll_zks);
        et_dlts = (EditText) findViewById(R.id.et_dlts);
        et_yyks = (EditText) findViewById(R.id.et_yyks);
        et_zks = (EditText) findViewById(R.id.et_zks);*/

        WDeep = ZSApplication.getInstance().getConfig1("WDeep");
        et_jdms.setText(WDeep);
        Mtype = ZSApplication.getInstance().getConfig1("Mtype");
        et_jggg.setText(Mtype);
        Wcover = ZSApplication.getInstance().getConfig1("Wcover");
        et_jgcz.setText(Wcover);
        Wdepth = ZSApplication.getInstance().getConfig1("Wdepth");
        et_jtsd.setText(Wdepth);
        Hsize = ZSApplication.getInstance().getConfig1("Hsize");
        et_jscc.setText(Hsize);
        BCode = ZSApplication.getInstance().getConfig1("BCode");
        et_qsdw.setText(BCode);
        RoadName = ZSApplication.getInstance().getConfig1("RoadName");
        et_dlmc.setText(RoadName);
        Subsid = ZSApplication.getInstance().getConfig1("Subsid");
        et_fsw.setText(Subsid);
        Feature = ZSApplication.getInstance().getConfig1("Feature");
        et_tezheng.setText(Feature);
        String bigName = ZSApplication.getInstance().getConfig("BigName");
        
        String jslx = ZSApplication.getInstance().getConfig1("jslx");
        et_jslx.setText(jslx);
        String tcsss = ZSApplication.getInstance().getConfig1("tcsss");
        et_tcsss.setText(tcsss);
        String tcsns = ZSApplication.getInstance().getConfig1("tcsns");
        et_tcsns.setText(tcsns);
        String jcjdj = ZSApplication.getInstance().getConfig1("jcjdj");
        et_jcjdj.setText(jcjdj);
        String jcjzpbh = ZSApplication.getInstance().getConfig1("jcjzpbh");
        et_jcjzpbh.setText(jcjzpbh);
        /*if ("DL".equals(bigName) || "TX".equals(bigName))
        {
            ll_dlts.setVisibility(View.VISIBLE);
            ll_yyks.setVisibility(View.VISIBLE);
            ll_zks.setVisibility(View.VISIBLE);
        }*/
    }

    private void initView()
    {
        getMyIntent();
        setOnclick();
        setSpinner();
    }

    private void setOnclick()
    {
        btnAccept.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
    }

    private void getMyIntent()
    {
        Intent intent = getIntent();
        specialLineList = (List<CmsSpecialLine>) intent.getExtras().getSerializable("wocaonima");
        mapNo = intent.getExtras().getString("mapNo");
        D_X = intent.getExtras().getString("D_X");
        D_Y = intent.getExtras().getString("D_Y");
        selectSubCode = intent.getExtras().getString("subCode");

        List<CmsPoints> pointsList = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsPointsDao().queryBuilder().where(Properties.PCode.eq(mapNo)).build().list();
        int pointsSize = pointsList.size();
        if (pointsSize == 0)
        {
            size = pointsList.size() + 1;
        }
        else
        {
            size = pointsList.get(pointsSize - 1).getMapNoNum() + 1;
        }
        et_gdbh.setText(mapNo + size);

        et_xzuobiao.setText(D_X);
        et_yzuobiao.setText(D_Y);
    }

    private void setSpinner()
    {
        tzItems = getResources().getStringArray(R.array.tz);
        fswItems = getResources().getStringArray(R.array.fsw);
        jgczItems = getResources().getStringArray(R.array.jgcz);
        qsdwItems = getResources().getStringArray(R.array.qsdw);
        ydqkItems = getResources().getStringArray(R.array.ydqk);

        /*
         * tzAdapter = new ArrayAdapter<String>(this,
         * android.R.layout.simple_spinner_item, tzItems);
         * tzAdapter.setDropDownViewResource
         * (android.R.layout.simple_spinner_dropdown_item);
         */
        tzAdapter = new FswAdapter(this);
        List<CmsFeature> tzlist = ZSApplication.getDaoSession("zyspps-db").getCmsFeatureDao().queryBuilder()
                .where(com.gesee.zyspps.db.CmsFeatureDao.Properties.SubCodes.like("%" + selectSubCode + "%"), com.gesee.zyspps.db.CmsFeatureDao.Properties.FiledName.eq("特征"))
                .orderAsc(com.gesee.zyspps.db.CmsFeatureDao.Properties.Sort).list();
        sp_tezheng.setAdapter(tzAdapter);
        tzAdapter.addData(tzlist);
        if(tzlist!=null&&tzlist.size()>0){
            for (int i = 0; i < tzlist.size(); i++)
            {
                if (tzlist.get(i).getFiledValue().toString().equals(Feature))
                {
                    sp_tezheng.setSelection(i);
                }
            }
        }
        

        /*
         * fswAdapter = new ArrayAdapter<String>(this,
         * android.R.layout.simple_spinner_item, fswItems);
         * fswAdapter.setDropDownViewResource
         * (android.R.layout.simple_spinner_dropdown_item);
         */
        fswAdapter = new FswAdapter(this);
        List<CmsFeature> fswlist = ZSApplication.getDaoSession("zyspps-db").getCmsFeatureDao().queryBuilder()
                .where(com.gesee.zyspps.db.CmsFeatureDao.Properties.SubCodes.like("%" + selectSubCode + "%"), com.gesee.zyspps.db.CmsFeatureDao.Properties.FiledName.eq("附属物"))
                .orderAsc(com.gesee.zyspps.db.CmsFeatureDao.Properties.Sort).list();
        sp_fsw.setAdapter(fswAdapter);
        fswAdapter.addData(fswlist);
        
        if(fswlist!=null&&fswlist.size()>0){
            for (int i = 0; i < fswlist.size(); i++)
            {
                if (fswlist.get(i).getFiledValue().toString().equals(Subsid))
                {
                    sp_fsw.setSelection(i);
                }
            }
        }
        
        List<CmsFeature> jgczlist = ZSApplication.getDaoSession("zyspps-db").getCmsFeatureDao().queryBuilder()
                .where(com.gesee.zyspps.db.CmsFeatureDao.Properties.SubCodes.like("%ALL%"), com.gesee.zyspps.db.CmsFeatureDao.Properties.FiledName.eq("井盖材质"))
                .orderAsc(com.gesee.zyspps.db.CmsFeatureDao.Properties.Sort).list();
        jgczAdapter = new FswAdapter(this);
        //jgczAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, jgczItems);
        //jgczAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_jgcz.setAdapter(jgczAdapter);
        jgczAdapter.addData(jgczlist);

        if(jgczlist!=null&&jgczlist.size()>0){
        	for (int i = 0; i < jgczlist.size(); i++)
            {
                if (jgczlist.get(i).getFiledValue().toString().equals(Wcover))
                {
                    sp_jgcz.setSelection(i);
                }
            }
        }
        
        List<CmsFeature> qsdwlist = ZSApplication.getDaoSession("zyspps-db").getCmsFeatureDao().queryBuilder()
                .where(com.gesee.zyspps.db.CmsFeatureDao.Properties.SubCodes.like("%ALL%"), com.gesee.zyspps.db.CmsFeatureDao.Properties.FiledName.eq("权属单位"))
                .orderAsc(com.gesee.zyspps.db.CmsFeatureDao.Properties.Sort).list();
        qsdwAdapter = new FswAdapter(this);
        //qsdwAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, qsdwItems);
        //qsdwAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_qsdw.setAdapter(qsdwAdapter);
        qsdwAdapter.addData(qsdwlist);

        if(qsdwlist!=null&&qsdwlist.size()>0){
        	for (int i = 0; i < qsdwlist.size(); i++)
            {
                if (qsdwlist.get(i).getFiledValue().toString().equals(BCode))
                {
                    sp_qsdw.setSelection(i);
                }
            }
        }
        
        List<CmsFeature> ydqklist = ZSApplication.getDaoSession("zyspps-db").getCmsFeatureDao().queryBuilder()
                .where(com.gesee.zyspps.db.CmsFeatureDao.Properties.SubCodes.like("%ALL%"), com.gesee.zyspps.db.CmsFeatureDao.Properties.FiledName.eq("淤堵情况"))
                .orderAsc(com.gesee.zyspps.db.CmsFeatureDao.Properties.Sort).list();
        ydqkAdapter = new FswAdapter(this);
        //ydqkAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ydqkItems);
        //ydqkAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_ydqk.setAdapter(ydqkAdapter);
        ydqkAdapter.addData(ydqklist);

        sp_tezheng.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                // TODO Auto-generated method stub
                // if (position == 0)
                // {
                // et_tezheng.setText("");
                // }
                // else
                // {
                if (parent.getItemAtPosition(position).toString().equals(Feature))
                {
                    et_tezheng.setText(parent.getItemAtPosition(position).toString());
                }
                if (flag2)
                {
                    et_tezheng.setText(parent.getItemAtPosition(position).toString());
                }
                flag2 = true;
               // et_tezheng.setText(parent.getItemAtPosition(position).toString());
                // }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub

            }
        });

        sp_fsw.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                // TODO Auto-generated method stub
                // if (position == 0)
                // {
                // et_fsw.setText("");
                // }
                // else
                // {
                if (parent.getItemAtPosition(position).toString().equals(Subsid))
                {
                    et_fsw.setText(parent.getItemAtPosition(position).toString());
                }
                if (flag3)
                {
                    et_fsw.setText(parent.getItemAtPosition(position).toString());
                }
                flag3 = true;
                //et_fsw.setText(parent.getItemAtPosition(position).toString());
                // }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub

            }
        });

        sp_jgcz.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                // TODO Auto-generated method stub
                //et_jgcz.setText(parent.getItemAtPosition(position).toString());
                if (parent.getItemAtPosition(position).toString().equals(Wcover))
                {
                	et_jgcz.setText(parent.getItemAtPosition(position).toString());
                }
                if (flag4)
                {
                	et_jgcz.setText(parent.getItemAtPosition(position).toString());
                }
                flag4 = true;
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub

            }
        });

        sp_qsdw.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                // TODO Auto-generated method stub
                // et_qsdw.setText(parent.getItemAtPosition(position).toString());
                if (parent.getItemAtPosition(position).toString().equals(BCode))
                {
                    et_qsdw.setText(parent.getItemAtPosition(position).toString());
                }
                if (flag)
                {
                    et_qsdw.setText(parent.getItemAtPosition(position).toString());
                }
                flag = true;
                Log.e("result", flag + "");
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub

            }
        });

        sp_ydqk.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                // TODO Auto-generated method stub
                et_ydqk.setText(parent.getItemAtPosition(position).toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub

            }
        });
    }

    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
        // 确定
        case R.id.btnAccept:
            String mapName = et_gdbh.getText().toString();
            
            if (StringUtil.isEmpty(mapName))
            {
                ToastUtil.show(context, "管点编号不能为空");
                break;
            }
            
            String mapD_X = et_xzuobiao.getText().toString();
            String mapD_Y = et_yzuobiao.getText().toString();
            String tezheng = et_tezheng.getText().toString();
            String fsw = et_fsw.getText().toString();
            String jdms = et_jdms.getText().toString();
            String jggg = et_jggg.getText().toString();
            String jgcz = et_jgcz.getText().toString();
            String jtsd = et_jtsd.getText().toString();
            String jscc = et_jscc.getText().toString();
            String qsdw = et_qsdw.getText().toString();
            String dlmc = et_dlmc.getText().toString();
            String bz = et_bz.getText().toString();
            String ydqk = et_ydqk.getText().toString();
            String jslx = et_jslx.getText().toString();
            String tcsss = et_tcsss.getText().toString();
            String tcsns = et_tcsns.getText().toString();
            String jcjdj = et_jcjdj.getText().toString();
            String jcjzpbh = et_jcjzpbh.getText().toString();
            /*String dlts = et_dlts.getText().toString();
            String yyks = et_yyks.getText().toString();
            String zks = et_zks.getText().toString();*/
            String dbName = ZSApplication.getInstance().getConfig("newDBName");

            String mapNameNum[] = mapName.split(mapNo);
            String MapNoNum = mapNameNum[1];

            List<CmsPoints> pointList = ZSApplication.getDaoSession(dbName).getCmsPointsDao().queryBuilder().where(Properties.D_X.eq(D_X), Properties.D_Y.eq(D_Y)).build().list();
            if (pointList.size() > 0)
            {
                ToastUtil.show(context, " 这个二点之间没有线  请先画线在插入点");
                break;
            }

            List<CmsPoints> pointsList = new ArrayList<CmsPoints>();

            pointsList = ZSApplication.getDaoSession(dbName).getCmsPointsDao().queryBuilder().where(Properties.ExpNO.eq(mapName)).build().list();
            
            if (pointsList.size() > 0)
            {
                ToastUtil.show(context, "管点编号已存在");
                break;
            }
            if(MapNoNum.indexOf("—")!=-1){
                MapNoNum = MapNoNum.substring(0,MapNoNum.indexOf("—"));
            }
            CmsPoints points = new CmsPoints();
            points.setExpNO(mapName);
            points.setPCode(mapNo);
            try {
            	points.setMapNoNum(Integer.parseInt(MapNoNum));
            	points.setD_X(Double.parseDouble(D_X));
                points.setD_Y(Double.parseDouble(D_Y));
                points.setFeature(tezheng);
                // points.setVisibility(kj);
                points.setSubsid(fsw);
                points.setWDeep(jdms);
                points.setMtype(jggg);
                points.setWcover(jgcz);
                // points.setMsize(jgcc);
                points.setWdepth(jtsd);
                points.setHsize(jscc);
                points.setBCode(qsdw);
                points.setRoadName(dlmc);
                /*points.setCableLines(dlts);
                points.setUseHole(yyks);
                points.setTotalHole(zks);*/
                points.setRemark(bz);
                points.setSblock(ydqk);
                points.setJslx(jslx);
                points.setTcsns(tcsns);
                points.setTcsss(tcsss);
                points.setJcjdj(jcjdj);
                points.setJcjzpbh(jcjzpbh);
                ZSApplication.getInstance().setConfig("jslx", jslx);
                ZSApplication.getInstance().setConfig("tcsns", tcsns);
                ZSApplication.getInstance().setConfig("tcsss", tcsss);
                ZSApplication.getInstance().setConfig("jcjdj", jcjdj);
                ZSApplication.getInstance().setConfig("jcjzpbh", jcjzpbh);
                ZSApplication.getInstance().setConfig("WDeep", jdms);
                ZSApplication.getInstance().setConfig("Subsid", fsw);
                ZSApplication.getInstance().setConfig("Feature", tezheng);
                ZSApplication.getInstance().setConfig("Mtype", jggg);
                ZSApplication.getInstance().setConfig("Wcover", jgcz);
                ZSApplication.getInstance().setConfig("Wdepth", jtsd);
                ZSApplication.getInstance().setConfig("Hsize", jscc);
                ZSApplication.getInstance().setConfig("BCode", qsdw);
                ZSApplication.getInstance().setConfig("RoadName", dlmc);
                ZSApplication.getInstance().setConfig("mapCurrentName", mapName);
                ZSApplication.getInstance().setConfig("mapCurrentD_X", mapD_X);
                ZSApplication.getInstance().setConfig("mapCurrentD_Y", mapD_Y);
                ZSApplication.getInstance().setConfig("mapCurrentfsw", fsw);
                ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsPointsDao().insert(points);
                if (specialLineList != null)
                {
                    ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsSpecialLineDao().delete(specialLineList.get(0));
                }
                ZSApplication.getInstance().finishActivity();
			} catch (Exception e) {
				// TODO: handle exception
				ToastUtil.show(context, "物探点号后面只能跟数字");
			}
            
            

            break;
        // 取消
        case R.id.btnCancel:
            ZSApplication.getInstance().setConfig("needDeleteSpeciaLine", "false");
            ZSApplication.getInstance().finishActivity();
            break;

        default:
            break;
        }
    }
}
